package com.djx.基础;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.junit.*;

import java.util.ArrayList;
import java.util.List;

public class ZKCreate {

    ZooKeeper zooKeeper = new MyZookepper01().zooKeeper;

    /**
     * ZooDefs.Ids.READ_ACL_UNSAFE ,读权限
     *
     */


    /**
     * 同步创建
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void syncCreate() throws KeeperException, InterruptedException {
        //arg1:节点的路径
        //arg2:节点的数据
        //arg3:权限列表 world:anyone:cdrwa
        //arg4:节点的类型  持久化节点
        System.out.println(zooKeeper.create("/aaa", "aaaValue".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    /**
     * 自定义权限创建节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void ziDingYiPermission01() throws KeeperException, InterruptedException {
        //world授权模式
        //权限列表
        List<ACL> acls = new ArrayList<ACL>();
        //授权模式和授权对象
        Id id = new Id("world", "anyone");
        //权限设置
        acls.add(new ACL(ZooDefs.Perms.READ, id));
        acls.add(new ACL(ZooDefs.Perms.WRITE, id));
        System.out.println(zooKeeper.create("/ooo/node04", "node04Value".getBytes(), acls, CreateMode.PERSISTENT));
    }

    @Test
    public void ziDingYiPermission02() throws KeeperException, InterruptedException {
        //ip授权模式
        //权限列表
        List<ACL> acls = new ArrayList<ACL>();
        //授权模式和授权对象
        Id id = new Id("ip", "192.168.15.132");
        //权限设置
        acls.add(new ACL(ZooDefs.Perms.ALL, id));

        System.out.println(zooKeeper.create("/ooo/node05", "node05Value".getBytes(), acls, CreateMode.PERSISTENT));
    }

    @Test
    public void ziDingYiPermission03() throws KeeperException, InterruptedException {
        //auth授权模式
        //添加授权用户
        zooKeeper.addAuthInfo("digest", "djx:123".getBytes());
        System.out.println(zooKeeper.create("/ooo/node07", "node06Value".getBytes(), ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT));
    }

    @Test
    public void ziDingYiPermission04() throws KeeperException, InterruptedException {
        //auth授权模式
        //添加授权用户
        zooKeeper.addAuthInfo("digest", "djx:123".getBytes());
        Id id = new Id("auth", "djx");
        List<ACL> acls = new ArrayList<ACL>();
        acls.add(new ACL(ZooDefs.Perms.READ, id));
        System.out.println(zooKeeper.create("/ooo/node08", "node06Value".getBytes(), acls, CreateMode.PERSISTENT));
    }

    @Test
    public void ziDingYiPermission05() throws KeeperException, InterruptedException {
        //digest授权模式
        //添加授权用户
        Id id = new Id("digest", "lxl:x/++yO1tq2AWazYDOfxqhQbYAbA=");
        List<ACL> acls = new ArrayList<ACL>();
        acls.add(new ACL(ZooDefs.Perms.READ, id));
        System.out.println(zooKeeper.create("/ooo/node09", "node06Value".getBytes(), acls, CreateMode.PERSISTENT));
    }

    /**
     * 持久化有序节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void ziDingYiPermissionPersistenceOrder01() throws KeeperException, InterruptedException {
        //持久化顺序节点
        //Ids.OPEN_ACL_UNSAFE world:any:cdrwa
        zooKeeper.create("/ooo/node10", "node10Value".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    }

    /**
     * 临时顺序节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void ziDingYiPermissionTemporaryOrder02() throws KeeperException, InterruptedException {
        System.out.println(zooKeeper.create("/ooo/node11", "node11Value".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL));
    }

    /**
     * 临时
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void ziDingYiPermissionTemporary03() throws KeeperException, InterruptedException {
        System.out.println(zooKeeper.create("/ooo/node12", "node12Value".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL));
        System.out.println(new String(zooKeeper.getData("/ooo/node12", false, null)));
        zooKeeper.close();
        System.out.println("zookeeper关闭");
        System.out.println("======================节点关闭后自动删除节点========================");
        System.out.println(zooKeeper.getData("/ooo/node12", false, null));

    }


    /**
     * 异步创建
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void asyncCreate() throws KeeperException, InterruptedException {
        zooKeeper.create("/ooo/node11", "node11Value".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new AsyncCallback.StringCallback() {
            //通过回调函数 确认是否创建成功
            //rc = 0 创建成功
            //path = 路径
            //name = 路径
            public void processResult(int rc, String path, Object ctx, String name) {
                System.out.println(rc);
                System.out.println(path);
                System.out.println(name);
                System.out.println(ctx);
            }
//           传递上下文对象  把这个对象传递到方法内部中去 ctx
        },"I am context");
        Thread.sleep(10000);
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
        System.out.println("因为是异步创建所以创建成功与否是要看第五个参数 回调接口来接收服务器给客户端的信息");
    }

    /**
     * 获取节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void getNode() throws KeeperException, InterruptedException {
        System.out.println(new String(zooKeeper.getData("/ooo/create2", false, null)));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }

    /**
     * 获取父节点中的子节点
     *
     * @throws KeeperException
     * @throws InterruptedException
     */
    @Test
    public void getChildNode() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/ooo", false, null);
        for (String child : children) {
            System.out.println("子节点内容为为" + child);
        }
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }


    @Test
    public void getAcl() throws KeeperException, InterruptedException {
        System.out.println(zooKeeper.getACL("/ooo/node0", null));
        zooKeeper.close();
        System.out.println("zookeeper连接关闭");
    }
}
